package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("login")
    public String login(User user, Model model){
        Integer code = userService.toLogin(user);// code = 1 查询到一条->登录成功；
        if (code==1){
            model.addAttribute("userName",user.getUserName());
            return "toIndex";
        }else {
            return "error_login";
        }

    }

    @RequestMapping("register")
    public String register(User user){
        Integer code = userService.toRegister(user);// code = 1 注册成功；
        if (code==1){//注册成功去登录
            return "login";
        }else {
            return "error_register";//注册失败去error页面
        }
    }
}
